#include <dlib/image_processing/frontal_face_detector.h>
#include <dlib/image_processing/render_face_detections.h>
#include <dlib/image_processing.h>
#include <dlib/dnn.h>
#include <dlib/image_io.h>
#include <dlib/data_io.h>
#include <iostream>

using namespace dlib;
using namespace std;

int main(int argc, char** argv) {

    //must be a parameter : image_path. check it ,and get image_path from argv
    if (argc != 2) {
        std::cout << "Usage: ./face_detector <image_path>" << std::endl;
        return -1;
    }

    //define image_path and set the value from argv[1]
    string image_path = argv[1];

    // 初始化人脸检测器
    frontal_face_detector detector = get_frontal_face_detector();

    // 加载人脸特征点检测器
    shape_predictor sp;
    deserialize("/home/ubuntu/practice/model/shape_predictor_68_face_landmarks.dat") >> sp;

    // 加载输入图像
    matrix<rgb_pixel> img;
    load_image(img, image_path);

    // 检测人脸
    std::vector<rectangle> faces = detector(img);

    // 检查是否检测到人脸
    if (faces.empty()) {
        cout << "No faces detected." << endl;
        return 1;
    }

    // 提取第一个检测到人脸的特征点并对齐
    auto shape = sp(img, faces[0]);
    matrix<rgb_pixel> face_chip;

    // 调用 extract_image_chip 提取对齐的人脸图像
    extract_image_chip(img, get_face_chip_details(shape, 150, 0.25), face_chip);

    // 保存对齐后的人脸图像
    save_jpeg(face_chip, "aligned_face.jpg");

    return 0;
}
